package top.maof.book.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.maof.book.model.History;
import top.maof.book.model.Role;
import top.maof.book.params.HistoryParam;
import top.maof.book.result.ApiResponse;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.result.Result;
import top.maof.book.service.HistoryService;
import top.maof.book.utils.JWTUtils;
import top.maof.book.vo.PageVo;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/history")
@Controller
@ResponseBody
public class HistoryController {
    @Autowired
    HistoryService historyService;

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("新建")
    @PostMapping("/create")
    public ApiResponse create(@RequestBody History history) {
        return ApiResponse.success(historyService.save(history));
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    public ApiResponse deleteById(@PathVariable Integer id) {
        historyService.removeById(id);
        return ApiResponse.success(null);
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    public ApiResponse deleteByIdInBatch(@RequestParam String ids) {
        String[] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Integer.valueOf(id));
        }
        historyService.removeBatchByIds(idList);
        return ApiResponse.success(null);
    }

    @RequiresRoles({Role.ADMIN})
    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    public ApiResponse updateById(@RequestBody History history) {
        historyService.updateById(history);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse findById(@PathVariable Integer id) {
        return ApiResponse.success(historyService.getById(id));
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse<PageData<History>> findPage(@RequestBody PageWrap<History> pageWrap) {
        return ApiResponse.success(historyService.findPage(pageWrap));
    }

    @GetMapping("profile")
    @Deprecated
    public Result profile(@RequestHeader("token") String token) throws Exception {
        Integer userId = JWTUtils.getUserId(token);
        return Result.success(historyService.findHistoriesByUser(userId));
    }


    @PostMapping("profile/list")
    public Result<PageVo> profileList(@RequestHeader("token") String token,
                                      @RequestBody HistoryParam historyParam) throws Exception {
        Integer userId = JWTUtils.getUserId(token);
        return Result.success();
    }


    @GetMapping("click")
    @Deprecated
    public Result clickCount(int bookId) throws Exception {
        return Result.success(historyService.clickCount(bookId));
    }


    @GetMapping("add")
    @Deprecated
    public Result add(History history, @RequestHeader("token") String token, HttpServletRequest request)
            throws Exception {
        history.setUserId(null);
        if (JWTUtils.checkToken(token)) {
            Integer userId = JWTUtils.getUserId(token);
            history.setUserId(userId);
        }
        history.setIp(request.getRemoteAddr());
        history.setInsertDate(new Date());
        int add = historyService.add(history);
        return Result.success();

    }

}
